package com.feng.show.controller;


import com.alibaba.fastjson.JSONObject;
import com.feng.show.comm.ResponseCode;
import com.feng.show.comm.ServerResponse;
import com.feng.show.entity.SecurityInfo;
import com.feng.show.service.ISecurityCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/security")
public class SecurityCodeController {

    @Autowired(required = true)
    ISecurityCodeService securityCodeServiceIpml;


    @PostMapping("/hello")
    public String  helloWord(){
        return "你好啊";
    }


    /*
    贴标签，将防伪码导入数据库,将防伪码和产品绑定：
    防伪码列表
    产品列表
    areaId,生产地id
    productId 不同类型的产品的id
    numberList;防伪码编号列表

     */

    @PostMapping("label")
    public ServerResponse<String>  label(

            @RequestParam(value = "userId") String userId,
            @RequestParam(value = "merchantId") String merchantId,
            @RequestParam(value = "numbers") String numbers,
            @RequestParam(value = "productId") String productId)
            
    {
        userId="10001";

        if(numbers==null||productId==null||merchantId==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"参数错误");

        }else{
            //调用插入数据
          //  DataSecurityCode signData = JSONObject.parseObject(numbers, DataSecurityCode.class);
            List<String> list=JSONObject.parseArray(numbers,String.class);
          //  List<String> list=signData.getNumbers();
            if(list==null||list.size()==0){
                return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"请选择标签");
            }
            return securityCodeServiceIpml.bindSecurityCode(userId,list,productId,merchantId);
        }

    }

    //根据安全码查询信息
    @GetMapping ("getAllInfoBySecurtyCode")
    public ServerResponse<SecurityInfo>  label(

            @RequestParam(value = "userId") String userId,
            @RequestParam(value = "number") String number)

    {
        userId="10001";

        if(number==null){
            return ServerResponse.createByErrorMsg("请输入防伪码");

        }else{
            //调用插入数据
            //  DataSecurityCode signData = JSONObject.parseObject(numbers, DataSecurityCode.class);

            //  List<String> list=signData.getNumbers();
//            if(list==null||list.size()==0){
//                return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"请选择标签");
//            }
//            return securityCodeServiceIpml.bindSecurityCode(userId,list,productId,areaId);

            return securityCodeServiceIpml.search(userId,number);
        }

    }






}
